package com.chuang.bootplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuang.bootplus.base.utils.ApiResponse;
import com.chuang.bootplus.base.utils.BeanUtil;
import com.chuang.bootplus.entity.Topic;
import com.chuang.bootplus.entity.TopicLike;
import com.chuang.bootplus.mapper.TopicLikeMapper;
import com.chuang.bootplus.mapper.TopicMapper;
import com.chuang.bootplus.po.topic.TopicLikeListPO;
import com.chuang.bootplus.po.topic.TopicLikeSavePO;
import com.chuang.bootplus.po.topic.TopicListPO;
import com.chuang.bootplus.service.TopicLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuang.bootplus.service.TopicService;
import com.chuang.bootplus.vo.TopicLikeListVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-08-17
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TopicLikeServiceImpl extends ServiceImpl<TopicLikeMapper, TopicLike> implements TopicLikeService {

    final TopicMapper topicService;

    @Override
    public ApiResponse<Void> save(TopicLikeSavePO po) {
        save(BeanUtil.beanA2beanB(po,TopicLike.class));
        return new ApiResponse<>();
    }

    @Override
    public ApiResponse<TopicLikeListVO> list(TopicLikeListPO po) {
        Page<TopicLike> page = page(
                new Page<TopicLike>(po.getPageNumber(),po.getPageSize()),
                new LambdaQueryWrapper<TopicLike>()
                        .eq(po.getTopicId()!=null,TopicLike::getTopicId,po.getTopicId())
                        .eq(po.getUserId()!=null,TopicLike::getUserId,po.getUserId())
        );
        List<TopicLikeListVO> topicList = BeanUtil.listA2ListB(page.getRecords(), TopicLikeListVO.class);
        for (TopicLikeListVO topicLikeListVO : topicList) {
            topicLikeListVO.setTopic(topicService.selectById(topicLikeListVO.getTopicId()));
        }
        return new ApiResponse<>(topicList,page.getTotal());
    }
}
